<?php
#注册插件
RegisterPlugin("os_access_right","ActivePlugin_os_access_right");
// 挂接口
function ActivePlugin_os_access_right() {
    Add_Filter_Plugin('Filter_Plugin_CreateOptoinsOfCategorys','os_ar_CreateOptoinsOfCategorys'); // 分类列表生成
    Add_Filter_Plugin('Filter_Plugin_PostArticle_Core','os_ar_PostArticle_Core'); // 提交的时候再验证一次
}

// 拦截分类生成
function os_ar_CreateOptoinsOfCategorys() {
    global $zbp;
    $checkStatus = false;
    // 锁定的权限高于除外
    switch ($zbp->user->Level) {
        case '2':
            if ($zbp->Config('os_access_right')->check_2) {
                $checkStatus = true;
            }
            $onlyCate = explode(',',$zbp->Config('os_access_right')->check_2);
        break;
        case '3':
            if ($zbp->Config('os_access_right')->check_3) {
                $checkStatus = true;
            }
            $onlyCate = explode(',',$zbp->Config('os_access_right')->check_3);
        break;
        case '4':
            if ($zbp->Config('os_access_right')->check_4) {
                $checkStatus = true;
            }
            $onlyCate = explode(',',$zbp->Config('os_access_right')->check_4);
        break;
    }
    if (!$checkStatus) {
        if ($zbp->user->Level > "3") {
            $outCate = array_merge(explode(',',$zbp->Config('os_access_right')->level_1),explode(',',$zbp->Config('os_access_right')->level_2),explode(',',$zbp->Config('os_access_right')->level_3));
        } else if ($zbp->user->Level > "2") {
            $outCate = array_merge(explode(',',$zbp->Config('os_access_right')->level_1),explode(',',$zbp->Config('os_access_right')->level_2));
        } else if ($zbp->user->Level > "1") {
            $outCate = explode(',',$zbp->Config('os_access_right')->level_1);
        }
        if ($zbp->user->Level > "1") {
            $subCate = array();
            foreach ($outCate as $cid) {
                if ($zbp->categorys[$cid]->SubCategorys) {
                    foreach ($zbp->categorys[$cid]->SubCategorys as $cate) {
                        $subCate[] = $cate->ID;
                    }
                }
            }
            $outCate = array_merge($outCate,$subCate);
            $array = array();
            foreach ($zbp->categorysbyorder as $id => $cate) {
                $isFlag = true;
                for ($i=0,$n=count($outCate);$i<$n;$i++) {
                    if ($outCate[$i] == $cate->ID) {
                        $isFlag = false;
                        break;
                    }
                }
                if ($isFlag) {
                    $array[] = $cate;
                }
            }
            $zbp->categorysbyorder = $array;
        }
    } else {
        $subCate = array();
        foreach ($onlyCate as $cid) {
            if ($zbp->categorys[$cid]->SubCategorys) {
                foreach ($zbp->categorys[$cid]->SubCategorys as $cate) {
                    $subCate[] = $cate->ID;
                }
            }
        }
        $onlyCate = array_merge($onlyCate,$subCate);
        $array = array();
        foreach ($zbp->categorysbyorder as $id => $cate) {
            $isFlag = false;
            for ($i=0,$n=count($onlyCate);$i<$n;$i++) {
                if ($onlyCate[$i] == $cate->ID) {$isFlag = true;break;}
            }
            if ($isFlag) {
                $array[] = $cate;
            }
        }
        $zbp->categorysbyorder = $array;
    }
}
// 提交时拦截一次，防止前端修改option
function os_ar_PostArticle_Core(&$object) {
    global $zbp;
    $checkStatus = false;
    // 锁定的权限高于除外
    switch ($zbp->user->Level) {
        case '2':
            if ($zbp->Config('os_access_right')->check_2) {
                $checkStatus = true;
            }
            $onlyCate = explode(',',$zbp->Config('os_access_right')->check_2);
        break;
        case '3':
            if ($zbp->Config('os_access_right')->check_3) {
                $checkStatus = true;
            }
            $onlyCate = explode(',',$zbp->Config('os_access_right')->check_3);
        break;
        case '4':
            if ($zbp->Config('os_access_right')->check_4) {
                $checkStatus = true;
            }
            $onlyCate = explode(',',$zbp->Config('os_access_right')->check_4);
        break;
    }
    if (!$checkStatus) {
        if ($zbp->user->Level > "3") {
            $outCate = array_merge(explode(',',$zbp->Config('os_access_right')->level_1),explode(',',$zbp->Config('os_access_right')->level_2),explode(',',$zbp->Config('os_access_right')->level_3));
        } else if ($zbp->user->Level > "2") {
            $outCate = array_merge(explode(',',$zbp->Config('os_access_right')->level_1),explode(',',$zbp->Config('os_access_right')->level_2));
        } else if ($zbp->user->Level > "1") {
            $outCate = explode(',',$zbp->Config('os_access_right')->level_1);
        }
        if ($zbp->user->Level > "1") {
            $subCate = array();
            foreach ($outCate as $cid) {
                if ($zbp->categorys[$cid]->SubCategorys) {
                    foreach ($zbp->categorys[$cid]->SubCategorys as $cate) {
                        $subCate[] = $cate->ID;
                    }
                }
            }
            $outCate = array_merge($outCate,$subCate);
            for ($i=0,$n=count($outCate);$i<$n;$i++) {
                if ($outCate[$i] == $object->CateID) {
                    $zbp->ShowError(6);
                    die();
                }
            }
        }
    } else {
        $subCate = array();
        foreach ($onlyCate as $cid) {
            if ($zbp->categorys[$cid]->SubCategorys) {
                foreach ($zbp->categorys[$cid]->SubCategorys as $cate) {
                    $subCate[] = $cate->ID;
                }
            }
        }
        $onlyCate = array_merge($onlyCate,$subCate);
        $isCheck = false;
        for ($i=0,$n=count($onlyCate);$i<$n;$i++) {
            if ($onlyCate[$i] == $object->CateID) {
                $isCheck = true;
                break;
            }
        }
        if (!$isCheck) {
            $zbp->ShowError(6);
            die();
        }
    }
}

function InstallPlugin_os_access_right() {
    global $zbp;
    if(!$zbp->Config('os_access_right')->HasKey('Version')){
        $array = array(
            'level_1'                   =>  '', // 只能是等级1以上才能发布的分类
            'level_2'                   =>  '', // 只能是等级2以上才能发布的分类
            'level_3'                   =>  '', // 只能是等级3以上才能发布的分类
            'check_2'                   =>  '', // 等级2只能在这些分类中发表
            'check_3'                   =>  '', // 等级3只能在这些分类中发表
            'check_4'                   =>  '', // 等级4只能在这些分类中发表
        );
        foreach ($array as $value => $intro) {
            $zbp->Config('os_access_right')->$value = $intro;
        }
    }
    $zbp->Config('os_access_right')->Version = '1.0';
    $zbp->SaveConfig('os_access_right');
}
function UninstallPlugin_os_access_right() {}
